/**
 * 异步请求获取添加信息
 */
function addInfo() {
    showAddRole()
}

/**
 * 异步请求获取编辑信息
 * @param id 角色序号
 */
function editRole(id) {
    $.ajax({
        type: "GET",
        url: "edit",
        data: "roleId=" + id,
        timeout: 3000,
        dataType: "json",
        success: function (res) {
            if(res!=null) {
                showEditRole(res);
            }else {
                alertMsg("操作失败！",null);
            }
        },
        error: function() {
            alertMsg("操作有误！",function() {window.location.reload();});
        }
    });
}
/**
 * 异步请求获取角色菜单信息
 * @param id 角色序号
 */
function assignMenu(id) {
    showAssignMenu(id);
    loadMenuTree(id);
}

/**
 * 显示角色添加信息弹窗
 */
function showAddRole() {
    //拼接表单内容
    var content = "<div class=\"w100\">\n" +
        "<form id=\"addFr\" action=\"/menu/edit\" method=\"post\">\n" +
        "<ul class=\"w95 fl\">\n" +
        "<li><span class=\"msg\"></span></li>\n" +
        "<li class='mt5'><span>角色名称:</span>\n" +
        "<input type=\"text\" name=\"roleName\" required=\"required\" placeholder=\"请输入角色名称\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n" +
        "<li class='mt5'><span>角色码:</span>\n" +
        "<input type=\"text\" name=\"roleCode\" placeholder=\"请输入角色码\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n"+
        "</ul>\n" +
        "<div class=\"clear\"></div>\n" +
        "</form>" +
        "</div>";
    $.confirm({
        title: '角色信息添加',
        content: content,
        confirmButtonClass: 'btn-info',
        animationSpeed: 1000,
        confirm: function () {
            var data = {};
            var formData = $('#addFr').serializeArray();
            $.each(formData, function() {
                data[this.name] = this.value;
            });
            $.ajax({
                type: "POST",
                url: "add",
                data: {
                    "roleId":0,
                    "roleName":data.roleName,
                    "roleCode":data.roleCode
                    },
                timeout: 3000,
                dataType: "text",
                success: function (res) {
                    if(res=="success") {
                        alertMsg("添加成功！",function() {window.location.reload();});
                    }else {
                        alertMsg("添加失败！",null);
                    }
                },
                error: function() {
                    alertMsg("操作有误！",null);
                }
            });
            return false;
        }
    });

}

/**
 * 显示角色编辑信息弹窗
 * @param data 角色数据
 */
function showEditRole(data) {
    //拼接表单内容
    var content = "<div class=\"w100\">\n" +
        "<form id=\"editFr\" action=\"/menu/edit\" method=\"post\">\n" +
        "<ul class=\"w95 fl\">\n" +
        "<li><span class=\"msg\"></span></li>\n" +
        "<li class='mt5'><span>角色名称:</span>\n" +
        "<input type=\"text\" name=\"roleName\" required=\"required\" value=\""+data.roleName+"\" placeholder=\"请输入角色名称\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n" +
        "<li class='mt5'><span>角色码:</span>\n" +
        "<input type=\"text\" name=\"roleCode\" value=\""+data.roleCode+"\" placeholder=\"请输入角色码\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n"+
        "</ul>\n" +
        "<div class=\"clear\"></div>\n" +
        "<div class=\"main_btn\">\n" +
        "<input type=\"hidden\" name=\"roleId\" value=\""+data.roleId+"\">\n" +
        "</div>\n" +
        "</form>" +
        "</div>";
    $.confirm({
        title: '角色信息修改',
        content: content,
        confirmButtonClass: 'btn-info',
        animationSpeed: 1000,
        confirm: function () {
            var data = {};
            var formData = $('#editFr').serializeArray();
            $.each(formData, function() {
                data[this.name] = this.value;
            });
            $.ajax({
                type: "POST",
                url: "edit",
                data: {
                    "roleId":data.roleId,
                    "roleName":data.roleName,
                    "roleCode":data.roleCode
                    },
                timeout: 3000,
                dataType: "text",
                success: function (res) {
                    if(res=="success") {
                        alertMsg("修改成功！",function() {window.location.reload();});
                    }else {
                        alertMsg("修改失败！",null);
                    }
                },
                error: function() {
                    alertMsg("操作有误！",null);
                }
            });
            return false;
        }
    });

}

/**
 * 显示分配菜单弹窗
 * @param id 角色菜单
 */
function showAssignMenu(id) {
    //拼接表单内容
    var content = "<div class=\"w100\">\n" +
        "<div>\n" +
        "<input id=\"checkAll\"  class=\"main_con_btn main_con_btn_pri fl\" type=\"button\" value=\"全选\" />\n" +
        "<input id=\"uncheckAll\"  class=\"main_con_btn main_con_btn_pri fl\" type=\"button\" value=\"取消全选\" />\n" +
        "<input id=\"expandAll\" class=\"main_con_btn main_con_btn_pri fl\" type=\"button\" value=\"展开全部\" />\n" +
        "<input id=\"collapseAll\" class=\"main_con_btn main_con_btn_pri fl\" type=\"button\" value=\"折叠全部\" />\n" +
        "</div>\n" +
        "<ul id=\"menuTree\" class=\"w95 fl ztree\"></ul>\n" +
        "<div class=\"clear\"></div>\n" +
        "</div>";
    $.confirm({
        title: '角色分配菜单',
        content: content,
        confirmButtonClass: 'btn-info',
        animationSpeed: 1000,
        confirm: function () {
            //拿到树的对象
            var zTree = $.fn.zTree.getZTreeObj("menuTree");
            //拿到选中的节点
            var nodes = zTree.getCheckedNodes(true);
            console.log(nodes);
            if (nodes.length == 0) {
                alertMsg("请选择需要分配的菜单",null);
            } else {
                var data = "roleId="+id;
                var menuIds = [];
                //将选中的菜单id拼接成参数字符串
                $.each(nodes, function(i, node){
                    menuIds.push(node.menuId);
                });
                data += "&menuIds="+menuIds;
                $.ajax({
                    type: "POST",
                    url: "assign",
                    data: data,
                    timeout: 3000,
                    dataType: "text",
                    success: function (res) {
                        if (res == "success") {
                            alertMsg("分配成功！", function () {
                                window.location.reload();
                            });
                        } else {
                            alertMsg("分配失败！", null);
                        }
                    },
                    error: function () {
                        alertMsg("操作有误！", null);
                    }
                });
            }
            return false;
        }
    });
}
/**
 * 加载菜单树状结构
 * @param id 角色序号
 */
function loadMenuTree(id) {
    var setting = {
        check: {
            enable: true
        },
        async: {
            enable: true,
            dataType:"json",
            type:"get",
            url:"/role/assign?roleId="+id
        }
    };
    $(function () {
        if ($("#menuTree")){
            //初始树结构
            $.fn.zTree.init($("#menuTree"), setting);
            //全选事件
            $("#checkAll").click(function () {
                var zTree = $.fn.zTree.getZTreeObj("menuTree");
                zTree.checkAllNodes(true);
            });
            //取消全选事件
            $("#uncheckAll").click(function () {
                var zTree = $.fn.zTree.getZTreeObj("menuTree");
                zTree.checkAllNodes(false);
            });
            //展开全部事件
            $("#expandAll").click(function () {
                var zTree = $.fn.zTree.getZTreeObj("menuTree");
                zTree.expandAll(true);
            });
            //折叠全部事件
            $("#collapseAll").click(function () {
                var zTree = $.fn.zTree.getZTreeObj("menuTree");
                zTree.expandAll(false);
            });
        }
    });
}
